package com.example.capsule_chat0322.request;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.example.capsule_chat0322.bean.ChatGroupFriendBean;
import com.example.capsule_chat0322.bean.FunctionBean;
import com.example.capsule_chat0322.bean.MessageBean;
import com.example.capsule_chat0322.bean.RoomBean;
import com.example.capsule_chat0322.configuration.HttpConstant;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.utils.AlterUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author zhuzhiming
 * @Date 2022/5/1 22:30
 */
public class RoomRequest {


    public static List<RoomBean> getRoomList(Context context, Handler handler, String userId, String macAddress, boolean auto) {

        final List<RoomBean>[] roomBeanList = new List[]{new ArrayList<>()};

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/getRoomList/" + userId + "/" + macAddress)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Looper.prepare();
                Toast.makeText(context, "获取聊天室列表失败", Toast.LENGTH_LONG).show();
                Log.e("error", "onFailure: 获取聊天室列表失败" + e);
                Looper.loop();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if(response.code() != 200){
                    return;
                }
                String json = response.body().string();
                List<RoomBean> rooms = new Gson().fromJson(json, new TypeToken<List<RoomBean>>() {
                }.getType());

                if (rooms == null || rooms.size() == 0) {
                    AlterUtils.alertByToastInSonThread(context, "您所在Wifi区域暂时没有聊天室");
                    return;
                }

                roomBeanList[0] = rooms;
                GlobalAssistMap.putObject("field", rooms.get(0).getField());
                handler.sendMessage(handler.obtainMessage(1, rooms));
                if (!auto) {
                    AlterUtils.alertByShortToastInSonThread(context, "刷新成功~");
                }
            }

        });
        return roomBeanList[0];
    }


    public static void getFunctionList(Context context, Handler handler, String roomName) {


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/getFunctionList/" + roomName)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                List<FunctionBean> functionBeans = new Gson().fromJson(json, new TypeToken<List<FunctionBean>>() {
                }.getType());

                if (functionBeans == null || functionBeans.size() == 0) {
                    AlterUtils.alertByToastInSonThread(context, "该聊天室暂时没有功能点");
                    return;
                }

                handler.sendMessage(handler.obtainMessage(1, functionBeans));
            }

        });

    }


    public static void getChatRoomFriend(Handler handler, String roomName) {


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/getChatRoomFriend/" + roomName)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() != 200) {
                    return;
                }
                String json = response.body().string();
                List<ChatGroupFriendBean> chatGroupFriendBeanList = new Gson().fromJson(json, new TypeToken<List<ChatGroupFriendBean>>() {
                }.getType());

                if (chatGroupFriendBeanList == null) {
                    return;
                }

                handler.sendMessage(handler.obtainMessage(1, chatGroupFriendBeanList));
            }

        });

    }


    public static void searchGroupFriend(Handler handler, String roomName, String content) {


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/searchGroupFriend/" + roomName + "/" + content)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() != 200) {
                    return;
                }
                String json = response.body().string();
                List<ChatGroupFriendBean> chatGroupFriendBeanList = new Gson().fromJson(json, new TypeToken<List<ChatGroupFriendBean>>() {
                }.getType());

                if (chatGroupFriendBeanList == null) {
                    return;
                }

                handler.sendMessage(handler.obtainMessage(1, chatGroupFriendBeanList));
            }

        });

    }


    public static void doStatisticPeople(Handler handler, String roomName, String imei) {


        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(HttpConstant.HTTP_PREFIX + "/statisticPeople/" + roomName + "/" + imei)
                .build();

        Call call = client.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.code() != 200) {
                    return;
                }
                String count = response.body().string();
                handler.sendMessage(handler.obtainMessage(1, count));
            }

        });

    }


    public static void addSecretRoom(MessageBean messageBean, Handler handler) {


        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , new Gson().toJson(messageBean));

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .post(requestBody)
                .url(HttpConstant.HTTP_PREFIX + "/addSecretRoom")
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.code() != 200) {
                    return;
                }

                String resBody = response.body().string();
                MessageBean messageBean = new Gson().fromJson(resBody, MessageBean.class);

                if (messageBean != null) {
                    handler.sendMessage(handler.obtainMessage(1, messageBean));
                }

            }
        });

    }


    public static void addFriend(MessageBean messageBean, Handler handler) {


        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , new Gson().toJson(messageBean));

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .post(requestBody)
                .url(HttpConstant.HTTP_PREFIX + "/addFriend")
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.code() != 200) {
                    return;
                }

                String resBody = response.body().string();
                MessageBean messageBean = new Gson().fromJson(resBody, MessageBean.class);

                if (messageBean != null) {
                    handler.sendMessage(handler.obtainMessage(1, messageBean));
                }

            }
        });

    }


}
